package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Resource
    private DeptService deptService;
    @Resource
    private EmpExprService exprService;

    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {
        //设置分页条件
        Page<Emp> page = Page.of(param.getPage(),  param.getPageSize());
        //排序
        page.addOrder(OrderItem.desc("update_time"));
        //增加查询条件
//        LambdaQueryWrapper<Emp> Wrapper=new LambdaQueryWrapper<>();
//        Wrapper.like(param.getName() != null && !param.getName().isEmpty() , Emp::getName , param.getName())
//                .eq(param.getGender() != null,Emp::getGender, param.getGender())
//                .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate, param.getBegin(), param.getEnd());
//
//        page = page(page,Wrapper);//查询分页数据
        page = lambdaQuery()
                .like(param.getName() != null && !param.getName().isEmpty() , Emp::getName , param.getName())
                .eq(param.getGender() != null,Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate, param.getBegin(), param.getEnd())
                .page(page);
        //查询到的员工数据
        List<Emp> empList = page.getRecords();
        //得到部门id
        List<Integer> deptIdList = empList.stream().map(emp -> emp.getDeptId()).toList();
        //查询部门数据
        List<Dept>deptList = deptService.listByIds(deptIdList);
        //把部门名封装到员工数据中
        empList.forEach(emp -> {
            deptList.forEach(dept -> {
                if (emp.getDeptId() != null &&emp.getDeptId().equals(dept.getId())) {
                    emp.setDeptName(dept.getName());
                }
            });
        });
        return new PageResult<>(page.getTotal(), empList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {
        //保存员工信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);
        //保存员工工作信息
        List<EmpExpr> exprList = emp.getExprList();
        //设置id
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        //批量储存工作经历信息
        exprService.saveBatch(exprList);
    }

    @Override
    public Emp getEmpById(Integer id) {
        //查询员工信息
        Emp emp = getById(id);
        //查询员工工作信息
        LambdaQueryWrapper<EmpExpr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId, id);
        List<EmpExpr> list = exprService.list(queryWrapper);
        emp.setExprList(list);
        return emp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmp(Emp emp) {
        //更新员工修改时间
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        //更新员工工作信息
        //删除员工工作信息
        LambdaQueryWrapper<EmpExpr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId, emp.getId());
        exprService.remove(queryWrapper);
        //批量保存员工工作信息
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        exprService.saveBatch(exprList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeEmpByIds(List<Integer> ids) {
        //批量删除员工信息
        removeBatchByIds(ids);
        //批量删除员工工作信息
        LambdaQueryWrapper<EmpExpr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EmpExpr::getEmpId, ids);
        exprService.remove(queryWrapper);
    }
}
